package com.cskaoyan.utils;

import com.cskaoyan.MybatisMain;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * 创建日期: 2022/08/01 14:51
 *
 * @author ciggar
 */
public class MybatisUtils {

    static SqlSessionFactory sqlSessionFactory;

    static {

        // 1. 读取核心配置文件
        InputStream in = null;
        try {
            in = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }


        // 2. 获取一个SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();


        // 3. 获取SqlSessionFactory
        sqlSessionFactory = sqlSessionFactoryBuilder.build(in);


    }


    // 获取SqlSession的方法
    public static SqlSession getSqlSession(Boolean autoCommit){

        SqlSession sqlSession = sqlSessionFactory.openSession(autoCommit);

        return sqlSession;

    }
}
